//
//  CompanyInviteCodeViewController.m
//  YaoHeAPP
//
//  Created by xyh on 16/7/5.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "CompanyInviteCodeViewController.h"
#import "Controller.h"
#import "Masonry.h"

@interface CompanyInviteCodeViewController ()<UITextFieldDelegate>
{
    NSInteger width;
    NSInteger height;

}
@property(nonatomic,strong)UITextField *yaoqingmaText;
@end

@implementation CompanyInviteCodeViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"RegistSuccess"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"RegistSuccess"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //调一个背景色
    UIColor*backColor=[UIColor whiteColor];
    UIColor*alphColor=[backColor colorWithAlphaComponent:0.96];
    self.view.backgroundColor=alphColor;
    
    width=self.view.frame.size.width;
    height=self.view.frame.size.height;
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(kNavigationBarHeight);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"公司邀请码";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(width/2-50);
        make.width.offset(100);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker*make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.bottom.offset(-12);
    }];
    
    
    //公司名称
    UIView *companyView=[[UIView alloc]init];
    companyView.frame=CGRectMake(0, 70, SCREEN_WIDTH, 40);
    companyView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:companyView];
    
    //提示标签
    UILabel*companyname = [[UILabel alloc]init];
    companyname.font = Font(15);
    companyname.text = @"邀请码:";
    companyname.frame = CGRectMake(20, 10, [self getTextwidthWithString:@"邀请码:"], 20);
    [companyView addSubview:companyname];
    
    //输入框
    _yaoqingmaText=[[UITextField alloc]init];
    _yaoqingmaText.font=Font(12);
    _yaoqingmaText.frame=CGRectMake(companyname.right+5, 10, SCREEN_WIDTH-companyname.right-5-10, 20);
    _yaoqingmaText.placeholder=@"请输入公司邀请码";
    _yaoqingmaText.clearButtonMode=UITextFieldViewModeWhileEditing;
    _yaoqingmaText.borderStyle=UITextBorderStyleNone;
    _yaoqingmaText.delegate=self;
    [_yaoqingmaText addTarget:self action:@selector(keyboardDissmiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [companyView addSubview:_yaoqingmaText];
    
    //创建公司
    UIButton*loginBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"加入公司" forState:UIControlStateNormal];
    UIImage*loginOldImg=[UIImage imageNamed:@"loginbutton"];
    UIImage*loginNewImg=[loginOldImg stretchableImageWithLeftCapWidth:15 topCapHeight:0];
    [loginBtn setBackgroundImage:loginNewImg forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(createCompany) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(companyView.mas_bottom).offset(10);
        make.left.offset(33);
        make.right.offset(-33);
        make.height.offset(40);
    }];

}
//加入公司
-(void)createCompany{

    NSDictionary*dic=@{@"COMPANY_ID":_yaoqingmaText.text};
    [Controller requestWithURL:@"AppCompanyRegist/PostBindCompany?from=app" jsondic:dic finishBlock:^(ClassData *classData) {
        if ([classData.number integerValue]==0) {
            UIAlertView*alertViewCT=[[UIAlertView alloc]initWithTitle:@"加入公司" message:@"加入公司成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            alertViewCT.tag=20;
            [alertViewCT show];
            NSString *IDnumString=@"123456";
            [[NSUserDefaults standardUserDefaults] setObject:IDnumString forKey:ISCREATE];
            NSString *isregtype=@"0";
            [[NSUserDefaults standardUserDefaults] setObject:isregtype forKey:ISREGTYPE];
            
        }
        else{
            ALERTSHOW(classData.message);
        }
    }];


}

//警告框的协议方法 下方两个按钮的索引
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==20) {
        RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
        [rootTabBarViewController changeRootViewControllerTo:0];
    }
}


//计算文字宽度（设置label的高度）
-(NSInteger)getTextwidthWithString:(NSString *)string
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT,38) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
    return rect.size.width+8;
}

//键盘弹下
-(void)keyboardDissmiss
{
    return;
}

//键盘弹下
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


-(void)backLogin{
    
   [self.navigationController popViewControllerAnimated:YES];

}
@end
